import 'package:acgn_pioneer/plugin/ushare/lib/ushare.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:uverify/uverify.dart';

import '../public.dart';

/// 启动页、闪屏广告页、引导页
class SplashPage extends StatefulWidget {
  SplashPage({Key key}) : super(key: key);

  @override
  SplashPageState createState() {
    return SplashPageState();
  }
}

class SplashPageState extends State<SplashPage> {
  // 基础config;
  BaseConfig config;
  // 前置用户获取
  UserProvider user;
  UserRecordProvider userRecord;

  LoginPage loginPage;
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      requestHanddle();
      initPlugin();
    });
  }

  initPlugin() {
    try {
      Ushare.init(
          appKey: AppConfig.umengAppKey,
          appSecret: AppConfig.umengAppSecret,
          wxAppid: AppConfig.WX_APPID,
          wxSecret: AppConfig.WX_SECRET,
          qqAppid: AppConfig.QQ_APPID,
          weiboAppid: AppConfig.WEIBO_APPID);
    } on PlatformException {}
  }

  requestHanddle() async {
    // 获取配置并设置配置
    ACGN.config = config = await HomeService.getGlobalConfig();
    setState(() {});
    var deviceInfo = await PlatformUtil.getDeviceInfo();
    // 获取用户信息
    String deviceId = deviceInfo[2];
    await loginPage
        .login({'type': 'device', 'device': deviceId, 'token': deviceId});

    // 设置用户信息
    // user.setUserInfo();
    // userRecord.setUserRecord();
    await Future.delayed(Duration(milliseconds: 3000));
    Routers.navigateTo(RouterPage.READ_PAGE, replace: true);
  }

  @override
  Widget build(BuildContext context) {
    loginPage = LoginPage(context);
    // user = Provider.of<UserProvider>(context);
    // userRecord = Provider.of<UserRecordProvider>(context);
    return Scaffold(
        backgroundColor: ColorUtil.rgba(27, 27, 38, 1),
        appBar: AppBar(
          automaticallyImplyLeading: false,
          // title: Text('闪屏'),
          centerTitle: true,
          leadingWidth: 44,
          leading: BackComponent(),
        ),
        body: Stack(alignment: Alignment.bottomCenter, children: <Widget>[
          Container(
              padding: EdgeInsets.all(0),
              decoration: BoxDecoration(
                  image: DecorationImage(
                      fit: BoxFit.fitWidth,
                      alignment: Alignment.topCenter,
                      image: AssetImage(
                          AppConfig.ASSETS_PATH + 'spbg_qmmh.jpg')))),
          Positioned(
              bottom: 30,
              child: Container(
                  width: ScreenUtil.getInstance().screenWidth,
                  child: Column(
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: [
                        _tempNavg(),
                        Image.asset(AppConfig.ASSETS_PATH + 'qmlogo_wz.png'),
                      ])))
        ]));
  }

  Widget _tempNavg() {
    return Container(
        margin: EdgeInsets.only(bottom: 10),
        child: config != null
            ? Wrap(spacing: 0, runSpacing: -12, children: [
                // _navNutton('阅读模块', context, RouterPage.READ_PAGE,
                //     {'comicId': 5323, 'chapterId': 1432639}),
                // _navNutton('我的页面', context, RouterPage.MY_INDEX),
                // _navNutton('我的账户', context, RouterPage.MY_ACCOUNT),
                // _navNutton('登录页面', context, RouterPage.LOGIN_PAGE),
                // _navNutton('用户注册', context, RouterPage.REGISTER_PAGE),
                // _navNutton('验码校验', context, RouterPage.VERIFICATION_CODE_PAGE, {
                //   'dialCode': '+86',
                //   'phone': '18123390165'
                // }),
                // _navNutton('分类页', context, RouterPage.SORT_MAIN_PAGE),
                _navNutton('测试页面', context, RouterPage.TEST),
                _navNutton('生命周期', context, RouterPage.TEST_LIFE),
                _navNutton('立即体验', context, RouterPage.HOME_PAGE),
              ])
            : Text(
                '正在加载配置…',
                style: TextStyle(color: Colors.white),
              ));
  }

  _navNutton(String title, context, route, [params]) {
    return OutlineButton(
      padding: EdgeInsets.all(0),
      onPressed: () {
        if (RouterPage.LOGIN_PAGE == route) {
          loginPage = LoginPage(context, size: MediaQuery.of(context).size);
          loginPage.init();
          return;
        }
        Routers.navigateTo(route, params: params);
      },
      child: Text(
        title,
        textAlign: TextAlign.center,
        style:
            TextStyle(fontSize: 14, color: ColorUtil.rgba(255, 255, 255, 0.3)),
      ),
    );
  }
}
